
package com.compuvest.webstat;

// <editor-fold defaultstate="collapsed" desc="import">
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
// </editor-fold>

//@WebServletContextListener
public class ContextInit implements ServletContextListener {

// <editor-fold defaultstate="collapsed" desc="init variables">

    public final static String VERSION = "webstat::ContextInit v1.0 2010-02-03";

    private ServletContext context;
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="contextInitialized()">

    @Override
    public void contextInitialized(final ServletContextEvent event) {

        StringBuilder out = new StringBuilder();
        out.append("Init: ").append(VERSION);

        context = event.getServletContext();
        try {
            // init mysql
            context.setAttribute("websql", new WMySQL());
            out.append("\n\twebsql ... Done");

            // init mail
            context.setAttribute("webmail", new WMail());
            out.append("\n\twmail ... Done");
	}
        catch(Exception ne) {
            System.out.println("contextInitialized() init error:\n" + ne);
        }

        System.out.println(out.toString());
    }
// </editor-fold>

// <editor-fold defaultstate="collapsed" desc="contextDestroyed()">

    @Override
    public void contextDestroyed(ServletContextEvent event) {

	context = event.getServletContext();
	context.removeAttribute("websql");
	context.removeAttribute("webmail");

        context = null;

	System.out.println("Destroy: " + VERSION);
    }
// </editor-fold>

}
